package com.qf.converter;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyConverter implements Converter<String, Date> {

    private String patten;

    public String getPatten(){
        return this.patten;
    }

    public void setPatten(String patten){
        this.patten=patten;
    }

    @Override
    public Date convert(String s) {
        //创建日期格式化的格式
        SimpleDateFormat dateFormat = new SimpleDateFormat(patten);
        try {
            Date parse=null;
            if (s.indexOf("T")==-1){
                parse = dateFormat.parse(s);
            }else {
                s=s.replace("T"," ");
                parse=dateFormat.parse(s);
            }
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
